package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.vo.SysAssetProcurementPlanVo;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysAssetProcurementPlanMapper;
import com.ruoyi.system.domain.SysAssetProcurementPlan;
import com.ruoyi.system.service.ISysAssetProcurementPlanService;
import org.springframework.util.CollectionUtils;

/**
 * 采购计划Service业务层处理
 * 
 * @author sxy
 * @date 2025-03-27
 */
@Service
public class SysAssetProcurementPlanServiceImpl implements ISysAssetProcurementPlanService 
{
    @Autowired
    private SysAssetProcurementPlanMapper sysAssetProcurementPlanMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询采购计划
     * 
     * @param planId 采购计划主键
     * @return 采购计划
     */
    @Override
    public SysAssetProcurementPlanVo selectSysAssetProcurementPlanByPlanId(Long planId)
    {
        SysAssetProcurementPlan sysAssetProcurementPlan = sysAssetProcurementPlanMapper.selectSysAssetProcurementPlanByPlanId(planId);
        SysAssetProcurementPlanVo rsp = new SysAssetProcurementPlanVo();
        BeanUtils.copyProperties(sysAssetProcurementPlan, rsp);
        if(sysAssetProcurementPlan.getApplicantId() != null){
            rsp.setApplicant(sysUserMapper.selectUserById(sysAssetProcurementPlan.getApplicantId()).getUserName());
        }
        if(sysAssetProcurementPlan.getReviewerId() != null){
            rsp.setReviewer(sysUserMapper.selectUserById(sysAssetProcurementPlan.getReviewerId()).getUserName());
        }
        if(sysAssetProcurementPlan.getHeadId() != null){
            rsp.setHead(sysUserMapper.selectUserById(sysAssetProcurementPlan.getHeadId()).getUserName());
        }

        return rsp;
    }

    /**
     * 查询采购计划列表
     * 
     * @param sysAssetProcurementPlan 采购计划
     * @return 采购计划
     */
    @Override
    public List<SysAssetProcurementPlanVo> selectSysAssetProcurementPlanList(SysAssetProcurementPlan sysAssetProcurementPlan)
    {
        List<SysAssetProcurementPlan> sysAssetProcurementPlans = sysAssetProcurementPlanMapper.selectSysAssetProcurementPlanList(sysAssetProcurementPlan);
        List<SysAssetProcurementPlanVo> sysAssetProcurementPlanVos = new ArrayList<>();
        if(!CollectionUtils.isEmpty(sysAssetProcurementPlans)){
            for (SysAssetProcurementPlan plan : sysAssetProcurementPlans) {
                SysAssetProcurementPlanVo planVo = new SysAssetProcurementPlanVo();
                BeanUtils.copyProperties(plan, planVo);
                //申请人
                if(plan.getApplicantId() != null){
                    planVo.setApplicant(sysUserMapper.selectUserById(plan.getApplicantId()).getUserName());
                }

                //审核人
                if(plan.getReviewerId() != null){
                    planVo.setReviewer(sysUserMapper.selectUserById(plan.getReviewerId()).getUserName());
                }

                //负责人
                if(plan.getHeadId() != null){
                    planVo.setHead(sysUserMapper.selectUserById(plan.getHeadId()).getUserName());
                }

                sysAssetProcurementPlanVos.add(planVo);
            }
        }
        return sysAssetProcurementPlanVos;
    }

    /**
     * 新增采购计划
     * 
     * @param sysAssetProcurementPlan 采购计划
     * @return 结果
     */
    @Override
    public int insertSysAssetProcurementPlan(SysAssetProcurementPlan sysAssetProcurementPlan)
    {
        sysAssetProcurementPlan.setCreateTime(DateUtils.getNowDate());
        //审核人随机
        List<Long> assetAdmin = sysUserMapper.selectRoleUser("asset_admin");
        sysAssetProcurementPlan.setReviewerId(assetAdmin.get(new Random().nextInt(assetAdmin.size())));
        //状态默认待审核
        sysAssetProcurementPlan.setReviewStatus("待审核");
        return sysAssetProcurementPlanMapper.insertSysAssetProcurementPlan(sysAssetProcurementPlan);
    }

    /**
     * 修改采购计划
     * 
     * @param sysAssetProcurementPlan 采购计划
     * @return 结果
     */
    @Override
    public int updateSysAssetProcurementPlan(SysAssetProcurementPlan sysAssetProcurementPlan)
    {
        return sysAssetProcurementPlanMapper.updateSysAssetProcurementPlan(sysAssetProcurementPlan);
    }

    /**
     * 批量删除采购计划
     * 
     * @param planIds 需要删除的采购计划主键
     * @return 结果
     */
    @Override
    public int deleteSysAssetProcurementPlanByPlanIds(Long[] planIds)
    {
        return sysAssetProcurementPlanMapper.deleteSysAssetProcurementPlanByPlanIds(planIds);
    }

    /**
     * 删除采购计划信息
     * 
     * @param planId 采购计划主键
     * @return 结果
     */
    @Override
    public int deleteSysAssetProcurementPlanByPlanId(Long planId)
    {
        return sysAssetProcurementPlanMapper.deleteSysAssetProcurementPlanByPlanId(planId);
    }
}
